package com.fas.hehe.system.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.fas.core.BaseController;
import com.fas.core.constant.R;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.core.plugin.tree.jstree.Node;
import com.fas.hehe.system.model.Organization;
import com.fas.hehe.system.service.OrganizationService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;

/**
 * 
 * @ClassName: OrganizationController
 * @Description: 组织架构管理
 * @Copyright: Copyright (c) 2016 FAS
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月25日 下午1:49:45
 * @version V1.0
 */
@RequiresAuthentication
@ControllerBind(controllerKey = "/system/organization", viewPath = "/system/organization")
public class OrganizationController extends BaseController {

	public OrganizationService organizationService;

	// 【异步】一次性加载字典分类树
	@RequiresPermissions(value = { R.Authority.Org.QUERY }, logical = Logical.AND)
	public void loadOrganizationTreeData() {
		List<Node> treeNodes = organizationService.loadTreeOnceTime(false);// 加载所有类型机构
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("treeNodes", treeNodes);
		this.returnJsonResult(returnData);
	}

	// 【异步】 根据组织机构ID查询组织机构信息（包含基本、子机构列表）
	@RequiresPermissions(value = { R.Authority.Org.QUERY }, logical = Logical.AND)
	public void loadOrganizationInfoById() {
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		Organization org = organizationService.findOrganizationById(this.getParaToLong("orgId"));
		List<Organization> subOrgs = organizationService.findSubOrganizationById(this.getParaToLong("orgId"));
		returnData.put("org", org);
		returnData.put("subOrgs", subOrgs);
		this.returnJsonResult(returnData);
	}

	// 【异步】 修改组织机构显示顺序
	@RequiresPermissions(value = { R.Authority.Org.UPDATE }, logical = Logical.AND)
	public void changeOrganizationPriority() {
		Long parentId = this.getParaToLong("parentId");
		String type = this.getPara("type");
		int orderby = this.getParaToInt("priority");
		boolean isSuccess = organizationService.changeOrganizationPriority(parentId, type, orderby);
		this.returnJsonResult(isSuccess);
	}

	// 【异步】 逻辑删除组织机构
	@RequiresPermissions(value = { R.Authority.Org.DELETE }, logical = Logical.AND)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_ORG_DATA)
	public void deleteOrganization() {
		boolean isSuccess = SUCCESS;
		String message = "";
		Long orgId = this.getParaToLong("orgId");
		if (organizationService.deleteOrganizationCheck(orgId)) {
			isSuccess = organizationService.deleteOrganization(orgId);
		} else {
			isSuccess = false;
			message = "删除失败！ 该组织机构存在关联子机构\\角色\\用户";
		}
		this.returnJsonResult(isSuccess, message, null);
	}

	// 【异步】保存组织机构（创建 & 修改）
	@RequiresPermissions(value = { R.Authority.Org.CREATE, R.Authority.Org.UPDATE }, logical = Logical.OR)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_ORG_DATA)
	public void saveOrganization() throws Exception {
		Organization organization = (Organization) this.getModelEntity();
		organization = organizationService.saveOrganization(organization);
		Node node = organizationService.buildJstreeNode(organization);
		this.returnJsonResult(node);
	}

	// 【异步】 根据ID查询组织机构
	@RequiresPermissions(value = { R.Authority.Org.QUERY }, logical = Logical.AND)
	public void findOrganizationById() {
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		Organization organization = (Organization) this.findById(this.getParaToLong("id"));
		Organization parentOrg = (Organization) this.findById(organization.getParentId());
		returnData.put("org", organization);
		returnData.put("parent_name", parentOrg != null ? parentOrg.getName() : "");
		this.returnJsonResult(returnData);
	}

}
